package com.d404.server;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.alibaba.fastjson.JSON;

import im.beans.Classify;
import im.beans.User;
import manager.PersistentManager;

/**
 * Servlet implementation class CreateClass
 * 创建分组
 */
//@WebServlet("/CreateClass")
public class CreateClass extends HttpServlet {
	private static final long serialVersionUID = 1L;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CreateClass() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("UTF-8");
		String userID = request.getParameter("userID");
		String userName = request.getParameter("userName");
		String _class = request.getParameter("_class");
		
		String status = "9";
		
		CreateClassBack ccb = new CreateClassBack();
		
		SessionFactory sf = PersistentManager.sharemanager().getSessionFactory();
//		Session session = sf.openSession();
		Session session = sf.getCurrentSession();
		Transaction tx = session.beginTransaction();
		try
		{
			
			// 检查此用户和此分组是否已经存在
			Query q = session.createQuery("from Classify cf where cf.ower = ? and cf.classifyName = ?");
			User u = new User();
			u.setId(Long.parseLong(userID));
			q.setParameter(0, u);
			q.setParameter(1, _class);
			List<Classify> cfs =  q.list();
			// 是否分组已经存在
			boolean isclassExist = false;
			for (Object obj:cfs) {
				isclassExist = true;
			}
			
			if (isclassExist) {
				ccb.setStatus(CreateClassBack.CLASS_EXISTS);
			}else
			{
				// 创建新分组
				Classify cf = new Classify();
				cf.setClassifyName(_class);
				cf.setFriendCount(0L);
				User user = new User();
//				user.setId(Long.parseLong(userID));
				user = (User) session.load(User.class, Long.parseLong(userID));
				cf.setOwer(user);
				cf.setOwerName(userName);
				
				session.save(cf);
				ccb.setStatus(CreateClassBack.SUCCESS);
				ccb.setClassID(cf.getId()+"");
				session.getTransaction().commit();
			}
			
		}catch(Exception e)
		{
			tx.rollback();
			ccb.setStatus(CreateClassBack.FAILED);
			e.printStackTrace();
		}finally {
//			session.close();
		}
		
		System.out.println(JSON.toJSON(ccb));
		response.getWriter().append(JSON.toJSONString(ccb));
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	/**
	 * 返回数据
	 * @author 凯
	 *
	 */
	public static class CreateClassBack
	{
		
	    public static final String CLASS_EXISTS = "2";
	    public static final String FAILED = "3";
	    public static final String SUCCESS = "1";
		
		public String status = "";
		public String classID = "";
		public String getStatus() {
			return status;
		}
		public void setStatus(String status) {
			this.status = status;
		}
		public String getClassID() {
			return classID;
		}
		public void setClassID(String classID) {
			this.classID = classID;
		}
		
	}

}
